﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using FinPlusCommon;
using log = Logger.Logger;
using u = FinPlusUtility.Utility;

namespace FinPlusWPFControls
{
    public partial class FinPlusColSelectBar : FinPlusControl
    {
        public override string Id { get; set; }
        public override string[] States { get { return new string[] { }; } }
        public override string[] TestStates { get { return new string[] { "IsValid" }; } }
        public override IFinPlusFunctions Model { get { return null; } }
        public override string ModelTest { get { return null; } }
        public override bool IsValid { get; set; }

        public event EventHandler<IFinPlusFunction> ColSelection;

        private FinPlusFuncStack _cmdStack = new FinPlusFuncStack();

        //construct
        public FinPlusColSelectBar()
        {
            InitializeComponent();
             
            IsValid = true;
        }

        //common control interface
        public override void Dock(object control, string dock)
        {
            IsValid = false;
            throw new NotImplementedException();
        }

        public override void Initialise()
        {

        }

        public override void CommandRouter(IFinPlusFunction func)
        {
            try
            {
                CaptureCommand(func);

                switch ((Commands)Enum.Parse(typeof(Commands), func.Name, true))
                {
                    case Commands.ColSelectionClear: SelectionClear(); break;
                    case Commands.ColSelection: _cmdStack.Last = func; Selection(func); break;
                    case Commands.ColSelectionUndo: Selection(_cmdStack.Last); break;
                    case Commands.ColSelectionRedo: Selection(_cmdStack.Next); break;

                    default: throw new Exception(string.Format("event not recognised {0}", func.Name));
                }
            }
            catch (Exception e)
            {
                IsValid = log.Log(Logger.Level.Warning, e.Message, e);
            }
        }

        public override void Dispose()
        {
            
        }

        public void AddButton(object value, bool isHidden)
        {
            ToggleButton button;
            var l = colSelector.Children.OfType<ToggleButton>().Where(b => b.Tag.Equals(value)).ToList();
            if (l.Count < 1)
            {
                button = new ToggleButton()
                {
                    Content = value.ToString(),
                    Name = u.Replace(value.ToString(), ".| |/|-", "_"), 
                    Tag = value, Width = 120, IsChecked = !isHidden, 
                    IsEnabled = true, Style = (Style)FindResource("ColSelectionToggleButtonStyle")
                };
                button.Click += Selection_Click;
                colSelector.Children.Add(button);
            }
            else
            {
                l[0].IsChecked = !isHidden;
            }
        }

        //private
        private void SelectionClear()
        {
            var buttons = colSelector.Children.OfType<ToggleButton>().Where(b => b.IsChecked == false).ToList();
            if (buttons.Count() > 0)
            {
                foreach (var b in buttons)
                {
                    b.IsChecked = true;
                    Selection_Click(b, new RoutedEventArgs());
                }
            }
            else
            {
                buttons = colSelector.Children.OfType<ToggleButton>().Where(b => b.IsChecked == true).ToList();
                foreach (var b in buttons)
                {
                    b.IsChecked = false;
                    Selection_Click(b, new RoutedEventArgs());
                }
            }
        }

        private void Selection(IFinPlusFunction func)
        {
            if (func != null)
            {
                if (ColSelection != null)
                    ColSelection.Invoke(this, (Function)func);
            }
        }

        //cmds
        private new enum Commands { ColSelection, ColSelectionClear, ColSelectionUndo, ColSelectionRedo }

        //events
        private void Selection_Click(object sender, RoutedEventArgs a) { CommandRouter(new Function(Commands.ColSelection.ToString(), Id, User, -1, ((ToggleButton)sender).Tag.ToString())); }
        private void SelectionClear_OnClick(object sender, RoutedEventArgs a) { CommandRouter(new Function(Commands.ColSelectionClear.ToString(), Id, User)); }
        private void SelectionUndo_OnClick(object sender, RoutedEventArgs a) { CommandRouter(new Function(Commands.ColSelectionUndo.ToString(), Id, User)); }
        private void SelectionRedo_OnClick(object sender, RoutedEventArgs a) { CommandRouter(new Function(Commands.ColSelectionRedo.ToString(), Id, User)); }
        private void Scroll_Up(object sender, MouseButtonEventArgs a) { scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset - 20); }
        private void Scroll_Down(object sender, MouseButtonEventArgs a) { scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset + 20); }
    }
}
